﻿namespace vKapse.VirtualDesktop.Core.UI
{

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;
	using System.Runtime.InteropServices;

	internal partial class DesktopSwitcherForm : Form
	{

		[StructLayout(LayoutKind.Sequential)]
		public struct MARGINS
		{
			public int cxLeftWidth;
			public int cxRightWidth;
			public int cyTopHeight;
			public int cyBottomHeight;
		}
		[DllImport("dwmapi.dll")]
		public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); 

		private Keys m_hotkey;

		public DesktopSwitcherForm()
		{
			this.InitializeComponent();
			this.DoubleBuffered = true;
			this.m_hotkey = Keys.Control | Keys.Space;
			
			HotKeyWrapper.HotKeyPressed += new KeyEventHandler(HotKeyWrapper_HotKeyPressed);
			HotKeyWrapper.Register(this.m_hotkey);

			Application.ApplicationExit += delegate
			{
				HotKeyWrapper.Unregister(this.m_hotkey);
			};
			this.Load += new EventHandler(DesktopSwitcherForm_Load);
		}

		void DesktopSwitcherForm_Load(object sender, EventArgs e)
		{
			this.Load -= new EventHandler(DesktopSwitcherForm_Load);
		}

		void HotKeyWrapper_HotKeyPressed(object sender, KeyEventArgs e)
		{
			if (e.KeyData == this.m_hotkey)
			{
				this.ShowHide();
			}
		}

		private void ShowHide()
		{
			if (this.Visible)
			{
				this.Visible = false;
				this.Hide();
				return;
			}
			this.UpdateUI();
			this.Visible = true;
			this.Show();
			this.Focus();

		}

		internal void UpdateUI()
		{
			Desktop[] desktops = DesktopManager.Instance.Desktops;
			// Set width & height
			this.Height = desktops[0].Thumbnail.Height + 26; // Set height
			this.Width = 10 + (desktops.Length * (desktops[0].Thumbnail.Width + 11));

			this.SuspendLayout();
			this.Controls.Clear();
			int pos = 0;
			foreach (Desktop desktop in desktops)
			{
				PictureBox desktopBox = new PictureBox();
				Label desktopLabel = new Label();
				desktopLabel.Text = desktop.Name;
				desktopLabel.AutoSize = false;
				desktopLabel.Width = desktop.Thumbnail.Width;
				desktopLabel.TextAlign = ContentAlignment.TopCenter;
				desktopLabel.AutoEllipsis = true;
				desktopLabel.Top = 1;
				desktopLabel.Height = 12;

				desktopBox.Top = 14;
				desktopBox.Left = (pos * (desktop.Thumbnail.Width + 10)) + 10;
				desktopLabel.Left = desktopBox.Left;
				desktopBox.Width = desktop.Thumbnail.Width;
				desktopBox.Height = desktop.Thumbnail.Height;
				desktopBox.Tag = desktop;
				desktopBox.Image = desktop.Thumbnail;
				desktopBox.Click += new EventHandler(desktopBox_Click);
				desktopBox.MouseEnter += new EventHandler(desktopBox_MouseEnter);
				desktopBox.MouseLeave += new EventHandler(desktopBox_MouseLeave);
				this.Controls.Add(desktopLabel);
				this.Controls.Add(desktopBox);
				++pos;
			}
			MARGINS margins = new MARGINS();
			margins.cxLeftWidth = 0;
			margins.cxRightWidth = 0;
			margins.cyTopHeight = this.Height;
			margins.cyBottomHeight = 0;
			DwmExtendFrameIntoClientArea(this.Handle, ref margins);
			this.ResumeLayout();
		}

		void desktopBox_MouseLeave(object sender, EventArgs e)
		{
			PictureBox desktopBox = (PictureBox)sender;
			desktopBox.BorderStyle = BorderStyle.None;
		}

		void desktopBox_MouseEnter(object sender, EventArgs e)
		{
			PictureBox desktopBox = (PictureBox)sender;
			desktopBox.BorderStyle = BorderStyle.Fixed3D;
		}

		void desktopBox_Click(object sender, EventArgs e)
		{
			this.ShowHide();
			PictureBox desktopBox = (PictureBox)sender;
			Desktop desktop = (Desktop)desktopBox.Tag;
			DesktopManager.Instance.Show(desktop);
		}
	}
}
